package com.gistmap.service.impl;

import com.gistmap.bean.Article;
import com.gistmap.dao.ArticleDao;
import com.gistmap.dto.ArticleDto;
import com.gistmap.dto.ArticleIndexDto;
import com.gistmap.service.ArticleService;
import com.gistmap.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/7.
 */

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;

    public void save(Article article) {
        articleDao.save(article);
    }

    public Pager<ArticleIndexDto> randomGet(Map map) {
        Pager pager = new Pager();
        int pageNo = Integer.parseInt(map.get("index").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        pager.setPageSize(pageSize);
        pager.setRowsTotal(articleDao.count(null));
        pager.setPageNo(pageNo);
        pageNo = pager.getPageNo();

        map.put("index", (pageNo-1)*pageSize);
        List<ArticleIndexDto> list = null;

        try {
            list = articleDao.randomGet(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pager.setList(list);

        return pager;
    }


    public Pager<Article> getByPage(Map map) {
        Pager pager = new Pager();
        int pageNo = Integer.parseInt(map.get("index").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        Integer uid = Integer.parseInt(map.get("uid").toString());
        System.out.println(uid);
        pager.setPageSize(pageSize);
        pager.setRowsTotal(articleDao.count(uid));
        pager.setPageNo(pageNo);
        pageNo = pager.getPageNo();
        if(pageNo == 0)
            pageNo = 1;
        map.put("index", (pageNo-1)*pageSize);
        List<Article> list = null;

        try {
            list = articleDao.getByPage(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pager.setList(list);

        return pager;
    }

    public ArticleDto get(String aid) {
        return articleDao.get(aid);
    }

    public Integer getArtCount(Integer id) {
        return articleDao.getArtCount(id);
    }

    public List<ArticleIndexDto> randomMain() {
        return articleDao.ramdomMain();
    }
}
